<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\Shop as ShopModel;
use app\admin\model\User;
use think\Db;
use think\Exception;

/**
 * 商户管理
 *
 * @icon   fa fa-list
 */
class Shop extends Backend
{

    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ShopModel();
    }


     /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams('title');
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => &$v) {
                $v['nickname'] = User::where(['id'=>$v['user_id']])->value('nickname');
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 审核操作
     *
     * @return string
     * @throws \think\Exception
     */
    public function update_status($id = null)
    {
        $status = $this->request->request('status');
        $remark = $this->request->request('remark','');
        $result = false;
        Db::startTrans();
        try {
            if($status == 1){
                $row = $this->model->where(['id'=>$id])->find();
                User::where(['id'=>$row['user_id']])->update(['is_shop'=>1]);
            }
            $result = $this->model->where(['id'=>$id])->update(['status'=>$status,'remark'=>$remark]);
            //创建后台admin账号
            


            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }

}